/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.passwords;

import org.json.JSONObject;
import org.webos.java.base.Dialog;
import org.webos.java.base.NetworkManager;
import org.webos.java.base.DialogType;
import org.webos.java.base.RemoteMessage;
import org.webos.java.root.Log;
/**
 *
 * @author kostya
 */
@DialogType(jsClass="passwords.CategorieEditor")
public class AddNewCategorie extends Dialog<PasswordManager>{

    private int id = 0;
    private boolean addNew = false;

    public AddNewCategorie(PasswordManager aManager,
            boolean aNewCategorie, String aName, int aID) {
        super(aManager);
        this.id = aID;
        this.addNew = aNewCategorie;
        getData().putOpt("new_cat", aNewCategorie);
        getData().putOpt("name", aNewCategorie?"":aName);
        getApplication().getSession().addDialog(this);
    }

    @RemoteMessage
    public JSONObject okClick(JSONObject aData){
        try {
            String name = aData.optString("name", "");
            if("".equals(name))
                return NetworkManager.getErrorObject(1003, "Name can't be empty");
            if(getApplication().categories.createOrEditCategorie(addNew, id, name)){
                Log.debug(this, "Closing AddNewCategorie");
                closeDialog(null);
                return null;
            }
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return NetworkManager.getErrorObject(1002, "Can't save folder");
    }
}
